package org.anno.chunkdb;

/**
 * Some {@link Host}s from the corresponding protocol buffers.
 */
public class Hosts {

  public static Host<TypedCellVisitor> from(final Metadata.Cell cell) {
    return visitor -> visitFor(visitor, cell);
  }

  public static Host<RowVisitor> from(final Metadata.Row row) {
    return new Host<RowVisitor>() {
      private int i;
      private final Host<TypedCellVisitor> cell =
          visitor -> visitFor(visitor, row.getCell(i));

      public void accept(RowVisitor visitor) {
        for (i = 0; i < row.getCellCount(); i++) {
          visitor.visit(i, Metadata.Column.Type.STRING, cell);
        }
      }
    };
  }

  private static void visitFor(TypedCellVisitor visitor, Metadata.Cell rowCell) {
    if (rowCell.hasStringValue()) {
      byte[] bytes = rowCell.getStringValueBytes().toByteArray();
      visitor.visitString(bytes, 0, bytes.length);
    }
  }
}
